package com.sys.dispatch;

import java.util.List;

import org.apache.log4j.Logger;

import com.sys.db.dao.DefFlowDao;
import com.sys.db.dao.DefJobDao;
import com.sys.db.dao.DefJobTypeDao;
import com.sys.db.dao.DefJobTypeParaDao;
import com.sys.db.dao.LogFlowDao;
import com.sys.db.dao.LogJobDao;
import com.sys.db.dao.RunFlowJobDao;
import com.sys.db.dao.RunJobParaDao;
import com.sys.db.util.ExtendDate;
import com.sys.db.util.GetBeanUtil;
import com.sys.pojo.DefFlow;
import com.sys.pojo.DefJob;
import com.sys.pojo.DefJobType;
import com.sys.pojo.DefJobTypePara;
import com.sys.pojo.LogFlow;
import com.sys.pojo.LogJob;
import com.sys.pojo.RunFlowJobPara;
import com.sys.pojo.RunJobPara;

public class HandFlowThread extends Thread{
	protected static Logger log = Logger.getLogger(FlowThread.class);
	private DefFlow df;
	private String dataDate;
	public HandFlowThread(DefFlow obj,String dataDate){
		this.df=obj;
		this.dataDate=dataDate;
	}
	public void run(){
		log.info("-----------------------手动调度线程开始--------------------------");
		DefFlowDao defDao=(DefFlowDao) GetBeanUtil.getDao("defFlowDaoImpl");//作业流dao
		LogFlowDao logFlowDao=(LogFlowDao) GetBeanUtil.getDao("logFlowDaoImpl");//作业流日志dao
		LogJobDao logJobDao=(LogJobDao) GetBeanUtil.getDao("logJobDaoImpl");//作业流日志dao
		RunFlowJobDao runFlowJobDao=(RunFlowJobDao) GetBeanUtil.getDao("runFlowJobDaoImpl");//作业流、作业关系dao
		DefJobDao defJobDao= (DefJobDao) GetBeanUtil.getDao("defJobDaoImpl");//作业dao
		DefJobTypeDao defJobTypeDao= (DefJobTypeDao) GetBeanUtil.getDao("defJobTypeDaoImpl");//作业类型dao
		DefJobTypeParaDao defJobTypeParaDao= (DefJobTypeParaDao) GetBeanUtil.getDao("defJobTypeParaDaoImpl");//作业参数dao
		RunJobParaDao runJobParaDao=(RunJobParaDao) GetBeanUtil.getDao("runJobParaDaoImpl");//作业参数值dao
		String message="";
		LogFlow lf=new LogFlow();
		DispatchUtil disUtil=new DispatchUtil();
		boolean success=true;
		log.info("作业流["+df.getFlowName()+"]开始执行！");
		try{
			while(Thread.interrupted()){
				throw new ExpServiceException("作业流["+df.getFlowName()+"]没有维护");
			}
			lf.setFlowId(df.getFlowId());
			lf.setDataDate(dataDate);
			lf.setRunSTime(ExtendDate.getSysDate());
			log.info("写入作业流日志！");
			logFlowDao.save(lf);//写日志表
			List flowJoblist=runFlowJobDao.findByFlowId(new Integer(df.getFlowId()));
			if(flowJoblist==null||flowJoblist.size()==0){
				throw new ExpServiceException("作业流["+df.getFlowName()+"]没有维护");
			}
			for(int i=0;i<flowJoblist.size();i++){
				RunFlowJobPara runFlowJob=(RunFlowJobPara) flowJoblist.get(i);
				DefJob dj=defJobDao.findById(new Integer(runFlowJob.getJobId()));
				if(dj.getJobType()!=null&&!"".equals(dj.getJobType())){
					log.info("查询作业["+dj.getJobName()+"]包含的参数和对应值");
					DefJobType djt=defJobTypeDao.findById(new Integer(dj.getJobType()));//作业类型
					if(djt==null){
						throw new ExpServiceException("作业["+dj.getJobName()+"]没有维护作业类型！");
					}
					List paralist=defJobTypeParaDao.findByTypeId(new Integer(dj.getJobType()));
					if(paralist==null||paralist.size()==0){
						throw new ExpServiceException("作业类型["+djt.getJobTypeName()+"]没有维护参数！");
					}else{
						List valueList=runJobParaDao.findByJobId(new Integer(runFlowJob.getJobId()));
						if(valueList.size()!=paralist.size()){
							throw new ExpServiceException("作业["+runFlowJob.getJobName()+"]参数维护有错误！");
						}
						StringBuffer sb=new StringBuffer();//组装执行程序的路径和参数
						sb.append(djt.getProgramPath()+djt.getProgramName()+" "+dataDate);
						for(int j=0;j<paralist.size();j++){
							DefJobTypePara a=(DefJobTypePara) paralist.get(j);
							RunJobPara b=runJobParaDao.findByTypeParaId(a.getId(),dj.getJobId());
							sb.append(" "+b.getJobParaValue());
						}
						log.info("调用后台程序命令："+sb.toString());
						if("BAT".equals(djt.getProgramType())){
							LogJob lj=new LogJob();
							lj.setDataDate(dataDate);
							lj.setFlowId(df.getFlowId());
							lj.setJobId(dj.getJobId());
							lj.setRunSTime(ExtendDate.getSysDate());
							logJobDao.save(lj);
							JavaExeMethod jem=new JavaExeMethod();
							boolean workFlag=false;
							workFlag=jem.exeBat(sb.toString());
							if(workFlag){
								//记录作业执行日志
								lj.setRunETime(ExtendDate.getSysDate());
								logJobDao.update(lj);
							}else{
								lj.setRunETime(ExtendDate.getSysDate());
								lj.setErrTxt("作业["+dj.getJobName()+"]执行失败");
								//记录作业错误日志
								logJobDao.update(lj);
								throw new ExpServiceException("作业["+dj.getJobName()+"]执行失败");
							}
						}
					}
				}else{
					throw new ExpServiceException("作业["+dj.getJobName()+"]不存在！");
				}
			}
			log.info("作业流["+df.getFlowName()+"]执行结束！");
		}catch (ExpServiceException e) {
			message=e.getMessage();
			log.info(message);
			success=false;
			lf.setErrTxt(message);
		}catch(InterruptedException e){
	    	System.out.println("线程停止了");
	    }catch (Exception e) {
			log.info(e.getMessage());
			message="系统异常！";
			success=false;
			lf.setErrTxt(message);
		}
		finally{
			try{
				if(success){
					lf.setStatus("0");
					df.setStatus("0");
					df.setAvgRun((int)disUtil.getAvgTimes(df, lf.getRunSTime()));
					df.setRuntimes(df.getRuntimes()+1);
				}else{
					lf.setStatus("2");
					df.setStatus("2");
				}
				lf.setRunETime(ExtendDate.getSysDate());
				logFlowDao.update(lf);
				defDao.update(df);
			} catch (Exception e1) {
				e1.printStackTrace();
			}
			log.info("-----------------------手动调度线程结束！--------------------------");
		}
	}
}
